package com.csun.cmny.provider.controller;


import com.csun.cmny.core.annotation.ValidateAnnotation;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.SecondLevelIdsDto;
import com.csun.cmny.provider.model.constant.ServiceOrderConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.FirstLevelIdsDto;
import com.csun.cmny.provider.model.dto.merchandise.MerchandiseQueryDto;
import com.csun.cmny.provider.model.dto.merchandise.MerchandiseSaveDto;
import com.csun.cmny.provider.model.dto.merchandise.MerchandiseUpdateDto;
import com.csun.cmny.provider.model.dto.merchandise.MerchandiseselectDto;
import com.csun.cmny.provider.model.vo.MerchandiseVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/merchandis", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMerchandiseController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcMerchandiseService merchandiseService;
    @Resource
    private OmcServiceFirstLevelService serviceFirstLevelService;
    @Resource
    private OmcServiceSecondLevelService secondLevelService;
    @Resource
    private OmcServiceOrderService serviceOrderService;
    @Resource
    private OmcServiceOrderItemService itemService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增商品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid MerchandiseSaveDto merchandiseSaveDto, BindingResult br) {

        //查看商品名是否冲突
        OmcMerchandise merchandise = new OmcMerchandise();
        merchandise.setUserId(getLoginAuthDto().getUserId());
        merchandise.setMerchandiseName(merchandiseSaveDto.getMerchandiseName());
        List<OmcMerchandise> select = merchandiseService.select(merchandise);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("merchandise.merchandiseName.error", null));
        }

        OmcMerchandise omcMerchandise = new OmcMerchandise();

        BeanUtils.copyProperties(merchandiseSaveDto,omcMerchandise);

        omcMerchandise.setGroupId(getLoginAuthDto().getGroupId());
        omcMerchandise.setUserId(getLoginAuthDto().getUserId());
        omcMerchandise.setOperator(getLoginAuthDto().getUserName());


        int save = merchandiseService.save(omcMerchandise);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改商品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid MerchandiseUpdateDto merchandiseUpdateDto, BindingResult br) {

        //查看商品名是否冲突
        OmcMerchandise merchandiseQuery = new OmcMerchandise();
        merchandiseQuery.setUserId(getLoginAuthDto().getUserId());
        merchandiseQuery.setMerchandiseName(merchandiseUpdateDto.getMerchandiseName());
        List<OmcMerchandise> select = merchandiseService.select(merchandiseQuery);

        if(PublicUtil.isNotEmpty(select)){
            for (OmcMerchandise merchandise : select) {
                if (!merchandise.getId().equals(merchandiseUpdateDto.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("merchandise.merchandiseName.error", null));
                }
            }
        }

        OmcMerchandise merchandise = merchandiseService.selectByKey(merchandiseUpdateDto.getId());
        if(merchandise.getIsPutaway().equals(0)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("merchandise.update.error", null));
        }

        OmcMerchandise omcMerchandise = new OmcMerchandise();


        BeanUtils.copyProperties(merchandiseUpdateDto,omcMerchandise);



        int result = merchandiseService.update(omcMerchandise);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询商品")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper select(@RequestBody @Valid MerchandiseQueryDto merchandiseQueryDto) {

        //个人手机注册账号默认机构为1001，应可通过商家id查询到商品信息
        //若角色为个人账号，可通过选择机构id查询该机构数据
        //若角色为机构管理员，默认查询该机构数据
        /*if(PublicUtil.isEmpty(merchandiseQueryDto.getGroupId())){
            merchandiseQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        }*/


        List<MerchandiseVo>list=merchandiseService.selectMerchandise(merchandiseQueryDto);

        PageInfo<MerchandiseVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商品上架")
    @RequestMapping(value = "/on", method = RequestMethod.POST)
    public Wrapper on(Long id) {

        OmcMerchandise omcMerchandise = new OmcMerchandise();

        omcMerchandise.setId(id);
        omcMerchandise.setIsPutaway(0);

        int update = merchandiseService.update(omcMerchandise);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商品下架")
    @RequestMapping(value = "/off", method = RequestMethod.POST)
    public Wrapper off(Long id) {

        OmcMerchandise omcMerchandise = new OmcMerchandise();

        omcMerchandise.setId(id);
        omcMerchandise.setIsPutaway(1);

        int update = merchandiseService.update(omcMerchandise);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "商品删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper delete(Long id) {

        OmcMerchandise merchandise = merchandiseService.selectByKey(id);

        if(merchandise.getIsPutaway()==0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("merchandise.isPutaway.error", null));
        }


        //校验是否有商品订单正在处理中
        OmcServiceOrderItem omcServiceOrderItem = new OmcServiceOrderItem();
        omcServiceOrderItem.setServiceThiredLevelId(id);
        List<OmcServiceOrderItem> select = itemService.select(omcServiceOrderItem);
        for (OmcServiceOrderItem serviceOrderItem : select) {
            OmcServiceOrder omcServiceOrder = new OmcServiceOrder();
            omcServiceOrder.setId(serviceOrderItem.getServiceOrderId());
            List<OmcServiceOrder> serviceOrders = serviceOrderService.select(omcServiceOrder);
            for (OmcServiceOrder serviceOrder : serviceOrders) {
                if (serviceOrder.getOrderStatus()!=ServiceOrderConstant.ORDER_STATUS_COMPELETE){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("merchandise.delete.error.isorder", null));
                }
            }
        }


        int delete = merchandiseService.deleteByKey(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询商品服务类别商家下的上架商品")
    @RequestMapping(value = "/selectmer", method = RequestMethod.POST)
    public Wrapper selectmer(@RequestBody @Valid MerchandiseselectDto merchandiseselectDto) {

        merchandiseselectDto.setIsPutaway(0);
        merchandiseselectDto.setGroupId(getLoginAuthDto().getGroupId());


        List<MerchandiseVo>list=merchandiseService.selectMerchandiseAndmerchant(merchandiseselectDto);

        PageInfo<MerchandiseVo> pageInfo = new PageInfo<>(list);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "判断商家名下是否有商品")
    @RequestMapping(value = "/haveMerchant", method = RequestMethod.POST)
    public Wrapper haveMerchant(Long id) {

        OmcMerchandise merchandise = new OmcMerchandise();
        merchandise.setMerchantId(id);

        List<OmcMerchandise> select = merchandiseService.select(merchandise);

        if (PublicUtil.isNotEmpty(select)) {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), true);
        } else {
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), false);
        }


    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "根据类别id查询类别名称")
    @RequestMapping(value = "/selectServiceFirstLevel", method = RequestMethod.POST)
    public Wrapper selectServiceFirstLevel(@RequestBody FirstLevelIdsDto firstLevelIdsDto) {

        List<OmcServiceFirstLevel> omcServiceFirstLevels = new ArrayList<>();
        for (Long aLong : firstLevelIdsDto.getFirstLevelIds()) {
            OmcServiceFirstLevel omcServiceFirstLevel = serviceFirstLevelService.selectByKey(aLong);

            omcServiceFirstLevels.add(omcServiceFirstLevel);
        }




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),omcServiceFirstLevels);

    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "根据二级服务类别id查询类别名称")
    @RequestMapping(value = "/selectServiceSecondLevel", method = RequestMethod.POST)
    public Wrapper selectServiceSecondLevel(@RequestBody SecondLevelIdsDto secondLevelIdsDto) {


        List<OmcServiceSecondLevel> serviceSecondLevels = new ArrayList<>();
        for (Long aLong : secondLevelIdsDto.getSecondLevelIds()) {
            OmcServiceSecondLevel omcServiceSecondLevel = secondLevelService.selectByKey(aLong);

            serviceSecondLevels.add(omcServiceSecondLevel);

        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),serviceSecondLevels);

    }


}
